﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Scenario_Editor.Windows
{
    public partial class GameAddinSelector : Form
    {
        #region Public Constructors

        public GameAddinSelector()
        {
            InitializeComponent();
        }

        #endregion Public Constructors

        #region Public Methods

        public string GetSelectedAddinname()
        {
            if (this.listBox_Addinnames.SelectedItem != null)
            {
                AddinNames addinname = (AddinNames)this.listBox_Addinnames.SelectedItem;
                return addinname.AddinName;
            }
            throw new Exception("No Addin selected in gameselector window.");
        }

        public void InitAddinNames(AddinManager.AddinManagerScenarioEditor addinmanger)
        {
            foreach (string addin in addinmanger.GetAddinNames())
            {
                this.listBox_Addinnames.Items.Add(new AddinNames() { Addin = addinmanger.GetAddin(addin), AddinName = addin });
            }

            this.listBox_Addinnames.SelectedIndex = -1;
        }

        public void SetAddin(string addinname)
        {
            addinname = addinname.ToUpper();
            for (int i = 0; i < this.listBox_Addinnames.Items.Count; i++)
            {
                if (this.listBox_Addinnames.Items[i].ToString().ToUpper() == addinname)
                {
                    this.listBox_Addinnames.SelectedIndex = i;
                    break;
                }
            }
        }

        #endregion Public Methods

        #region Private Methods

        private void listBox_Addinnames_DoubleClick(object sender, EventArgs e)
        {
            if (this.listBox_Addinnames.SelectedItem != null)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        #endregion Private Methods

        #region Private Structs

        private struct AddinNames
        {
            #region Public Properties

            public AddinManager.IAddinScenarioEditor Addin { get; set; }
            public string AddinName { get; set; }

            #endregion Public Properties

            #region Public Methods

            public override string ToString()
            {
                return this.AddinName;
            }

            #endregion Public Methods
        }

        #endregion Private Structs
    }
}